﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TheOutLetLibrary.Dominio;
using System.Data.OleDb;

namespace TheOutLetLibrary.Infraestrutura {
    public class Accessk_imageDAO : BaseDAO, IGenericDAO<k_image> {

        public Accessk_imageDAO(OleDbTransaction transacao)
            : base(transacao) {
        }

        public int Inclua(k_image entity) {

            OleDbCommand comando = GetCommand();
            comando.CommandText =
                " INSERT INTO k_image (" +
                " ProductId, ImageName, Type, Link) " +
                " VALUES (" +
                " @ProductId, @ImageName, @Type, @Link) ";
            comando.Parameters.Add("@ProductId", OleDbType.Integer).Value = entity.ProductId;
            comando.Parameters.Add("@ImageName", OleDbType.VarChar).Value = entity.ImageName;
            comando.Parameters.Add("@Type", OleDbType.VarChar).Value = entity.Type;
            comando.Parameters.Add("@Link", OleDbType.VarChar).Value = entity.Link;
            comando.ExecuteNonQuery();

            comando.CommandText = "SELECT @@IDENTITY";
            entity.COD = Convert.ToInt32(comando.ExecuteScalar());
            return entity.COD;
        }

        public k_image Ache(int Id) {

            OleDbCommand comando = GetCommand();
            comando.CommandText = "SELECT * FROM k_image WHERE Id = @Id";
            comando.Parameters.Add("@Id", OleDbType.Integer).Value = Id;
            OleDbDataReader reader = comando.ExecuteReader();
            if(reader.HasRows) {
                return new k_image {
                    ImageName = reader.GetString(reader.GetOrdinal("ImageName")),
                    Link = reader.GetString(reader.GetOrdinal("Link")),
                    Type = reader.GetString(reader.GetOrdinal("Type")),
                    ProductId = reader.GetInt32(reader.GetOrdinal("ProductId"))
                };
            } else {
                return null;
            }
        }
    }
}
